﻿using System;
using System.Net;
using AppServer.Core;

namespace AppServer.Implement.Socket
{
    /// <summary>
    /// Socket服务器配置
    /// </summary>
    public class SocketConfig:IServerConfig
    {
        /// <summary>
        /// 构造函数（UDP）
        /// </summary>
        /// <param name="endpoint">终结点</param>
        /// <param name="buffSize">缓冲区大小</param>
        /// <param name="receiveFilter">Receive过滤</param>
        public SocketConfig(IPEndPoint endpoint, int buffSize,IFilter receiveFilter)
            : this(SocketMode.UDP, endpoint, 1, buffSize,null, null, receiveFilter)
        {

        }

        /// <summary>
        /// 构造函数（TCP）
        /// </summary>
        /// <param name="endpoint">终结点</param>
        /// <param name="maxSessionCount">最大Session数</param>
        /// <param name="buffSize">缓冲区大小</param>
        /// <param name="keeplive">KeepLiveArgs</param>
        /// <param name="registFilter">Regist过滤</param>
        public SocketConfig(IPEndPoint endpoint, int maxSessionCount,int buffSize, KeepLiveArgs keeplive, IFilter registFilter)
            : this(SocketMode.TCP, endpoint, maxSessionCount, buffSize,keeplive,registFilter,null)
        {

        }

        internal SocketConfig(SocketMode mode, 
            IPEndPoint endpoint,
            int maxSessionCount,
            int buffSize,
            KeepLiveArgs keeplive,
            IFilter registFilter,
            IFilter receiveFilter)
        {
            this.Mode = mode;
            this.EndPoint = endpoint;
            this.MaxSessionCount = maxSessionCount;
            this.BuffSize=buffSize;
            this.KeepLive = keeplive;
            this.RegistFilter = registFilter;
            this.ReceiveFilter = receiveFilter;
        }


        /// <summary>
        /// Socket模式
        /// </summary>
        public SocketMode Mode
        {
            get;
            private set;
        }

        /// <summary>
        /// 监听结点
        /// </summary>
        public IPEndPoint EndPoint
        {
            get;
            private set;
        }

        /// <summary>
        /// 最大Session数
        /// </summary>
        public int MaxSessionCount
        {
            get;
            private set;
        }

        /// <summary>
        /// 缓冲区大小
        /// </summary>
        public int BuffSize
        {
            get;
            private set;
        }

        /// <summary>
        /// KeepLive
        /// </summary>
        public KeepLiveArgs KeepLive
        {
            get;
            private set;
        }

        /// <summary>
        /// KeepLiveArgs
        /// </summary>
        public class KeepLiveArgs
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="milliSeconds">间隔时间（毫秒）</param>
            /// <param name="enable">是否开启</param>
            public KeepLiveArgs(int milliSeconds, bool enable = true)
            {
                this.Enable = enable;
                this.MilliSeconds = milliSeconds;
            }

            /// <summary>
            /// 是否启用
            /// </summary>
            public bool Enable
            {
                get;
                private set;
            }

            /// <summary>
            /// 间隔时间（毫秒）
            /// </summary>
            public int MilliSeconds
            {
                get;
                private set;
            }

            /// <summary>
            /// 获取KeepLive参数
            /// </summary>
            public byte[] GetOptionValues()
            {
                byte[] inOptionValues = new byte[4 * 3];
                BitConverter.GetBytes((UInt32)(this.Enable?1:0)).CopyTo(inOptionValues, 0);
                BitConverter.GetBytes((UInt32)5000).CopyTo(inOptionValues, 4);
                BitConverter.GetBytes((UInt32)this.MilliSeconds).CopyTo(inOptionValues, 8);
                return inOptionValues;
            }
        }

        /// <summary>
        /// RegistSession过滤
        /// </summary>
        public IFilter RegistFilter
        {
            get;
            private set;
        }

        /// <summary>
        /// Receive过滤
        /// </summary>
        public IFilter ReceiveFilter
        {
            get;
            private set;
        }
    }
}
